﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Dzzzr.DataLayer;
using Dzzzr.Utils;

namespace Dzzzr
{
  public partial class Entity
  {
    [DataObjectMethod(DataObjectMethodType.Select)]
    public static List<Entity> GetGameEntityList(int gameID)
    {
      List<Entity> entities = CBO.FillCollection<Entity, List<Entity>>(typeof(Entity),Helper.DB.ExecuteReader("GetEntityListByGameID", gameID));
      return entities;
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public static List<Entity> GetTeamEntityList(int teamID)
    {
      List<Entity> entities = new List<Entity>();
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetEntityListByTeamID", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TeamID", teamID);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (rd.Read())
        {
          Entity entity = new Entity();
          entity.ID = rd.GetInt32(0);
          entity.Name = rd.GetString(1);
          entity.Description = rd.GetString(2);
          entity.TeamID = rd.GetInt32(3);
          entity.EntityTypeID = rd.GetInt32(4);
          entity.EntityTypeName = rd.GetString(5);
          entities.Add(entity);
        }
      }
      return entities;
    }

    [DataObjectMethod(DataObjectMethodType.Insert)]
    public static void InsertEntity(int TeamID, string Name, string Description, int EntityTypeID)
    {
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("InsertEntity", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", Name);
        cmd.Parameters.AddWithValue("@TeamID", TeamID);
        cmd.Parameters.AddWithValue("@EntityTypeID", EntityTypeID);
        cmd.Parameters.AddWithValue("@Photo", null);
        cmd.Parameters.AddWithValue("@Description", Description);
        cmd.ExecuteNonQuery();
      }
    }

    [DataObjectMethod(DataObjectMethodType.Delete)]
    public static void DeleteEntity(int ID)
    {
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("DeleteEntity", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", ID);
        cmd.ExecuteNonQuery();
      }
    }

    public static Entity GetEntityByID(int id)
    {
      Entity entity = null;
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetEntityByID", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ID", id);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if(rd.Read())
        {
          entity = new Entity();
          entity.ID = rd.GetInt32(0);
          entity.Name = rd.GetString(1);
          entity.Description = rd.GetString(2);
          entity.TeamID = rd.GetInt32(3);
          entity.EntityTypeID = rd.GetInt32(4);
          entity.EntityTypeName = rd.GetString(5);
        }
      }
      return entity;
    }

    public static Entity GetEntityByName(int teamID, string name)
    {
      Entity entity = null;
      using (SqlConnection cnn = Helper.Connection)
      {
        SqlCommand cmd = new SqlCommand("GetEntityByName", cnn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@TeamID", teamID);
        cmd.Parameters.AddWithValue("@Name", name);
        SqlDataReader rd = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (rd.Read())
        {
          entity = new Entity();
          entity.ID = rd.GetInt32(0);
          entity.Name = rd.GetString(1);
          entity.Description = rd.GetString(2);
          entity.TeamID = rd.GetInt32(3);
          entity.EntityTypeID = rd.GetInt32(4);
          entity.EntityTypeName = rd.GetString(5);
        }
      }
      return entity;
    }
    
  }
}
